package com.yanqu.road.logcollector.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.logcollector.log.CKLogMgr;
import com.yanqu.road.logic.model.ck.CkLogParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/ckLog")
public class LogController {

    private static Logger logger = LogManager.getLogger(LogController.class);

    @RequestMapping(value = "/serverCollect")
    public String serverCollect(@RequestBody String paramStr, HttpServletRequest request) {
//        logger.info("收到服务端日志 ipAddress:{}", request.getRemoteAddr());
        List<CkLogParam> ckLogParamList = JSON.parseObject(paramStr, new TypeReference<List<CkLogParam>>() {
        });

        if (!ckLogParamList.isEmpty()) {
            for (CkLogParam ckLogParam : ckLogParamList) {
                CKLogMgr.addCKLog(ckLogParam.getClassName(), ckLogParam.getSql(), ckLogParam.getDataList());
            }
        }
        return "success";
    }

    @GetMapping(value = "/query")
    @ResponseBody
    public String queryLog(@RequestParam("sql") String sql) {

        return "success";
    }
}
